//
//  RCChangePointsDetailDataModel.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-11-24.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCChangePointsDetailDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NSArray+EX.h"

@implementation RCChangePointsExtraItem

@synthesize title = _title;
@synthesize changeNum = _changeNum;
@synthesize userName = _userName;
@synthesize content = _content;
@synthesize score = _score;
@synthesize dateTime = _dateTime;

-(id)init{
    self = [super init];
    if (self) {
        _title = nil;
        _changeNum = nil;
        _userName = nil;
        _content = nil;
        _score = 0;
        _dateTime = nil;
    }
    return self;
}

-(void)dealloc{
    self.title = nil;
    self.changeNum = nil;
    self.userName = nil;
    self.content = nil;
    self.dateTime = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCChangePointsExtraItem *retObject = nil;
    retObject = [[RCChangePointsExtraItem alloc]init];
    if (nil == retObject) {
        return nil;
    }
    retObject.title = [data stringValueForKey:@"title" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.changeNum = [data stringValueForKey:@"changenum" defaultValue:nil operation:NSStringOperationTypeTrim];
    if (nil == retObject.changeNum) {
        retObject.changeNum = [data stringValueForKey:@"reviewnum" defaultValue:nil operation:NSStringOperationTypeTrim];
    }
    retObject.userName = [data stringValueForKey:@"username" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.content = [data stringValueForKey:@"content" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.dateTime = [data stringValueForKey:@"date" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.score = [data intValueForKey:@"score" defaultValue:0];
    return [retObject autorelease];
}

@end

@implementation RCChangePointsDetailDataModel

@synthesize shopID = _shopID;
@synthesize title = _title;
@synthesize thumb = _thumb;
@synthesize remain = _remain;
@synthesize price = _price;
@synthesize codeNumber = _codeNumber;
@synthesize points = _points;
@synthesize changetimes = _changetimes;
@synthesize score = _score;
@synthesize picExtraArray = _picExtraArray;
@synthesize reviewItem = _reviewItem;
@synthesize changeRecordItem = _changeRecordItem;

-(id)init{
    self = [super init];
    if (self) {
        _shopID = nil;
        _title = nil;
        _thumb = nil;
        _remain = nil;
        _price = nil;
        _codeNumber = nil;
        _points = nil;
        _changetimes = nil;
        _score = 0;
        _picExtraArray = nil;
        _reviewItem = nil;
        _changeRecordItem = nil;
    }
    
    return self;
}

-(void)dealloc{
    self.shopID = nil;
    self.title = nil;
    self.thumb = nil;
    self.remain = nil;
    self.price = nil;
    self.codeNumber = nil;
    self.points = nil;
    self.changetimes = nil;
    self.picExtraArray = nil;
    self.reviewItem = nil;
    self.changeRecordItem = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    
    NSDictionary *dict = [data dictionaryValueForKey:@"data" defaultValue:nil];
    if (nil == dict) {
        return nil;
    }
    RCChangePointsDetailDataModel *retObject = nil;
    retObject = [[RCChangePointsDetailDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    
    retObject.shopID = [dict stringValueForKey:@"goodsid" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.title = [dict stringValueForKey:@"title" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.thumb = [dict stringValueForKey:@"thumb" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.remain = [dict stringValueForKey:@"remain" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.price = [dict stringValueForKey:@"price" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.codeNumber = [dict stringValueForKey:@"codenumber" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.points = [dict stringValueForKey:@"points" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.changetimes = [dict stringValueForKey:@"changetimes" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.score = [dict intValueForKey:@"score" defaultValue:0];
    
    NSArray *dictArray = [dict arrayValueForKey:@"items" defaultValue:nil];
    if (dictArray != nil && [dictArray count] > 0) {
        NSMutableArray *picTempArray = [[NSMutableArray alloc]init];
        
        for (int i = 0; i < [dictArray count]; i++) {
            NSDictionary *dictItem = [dictArray safeObjectAtIndex:i];
            NSString *type = [dictItem stringValueForKey:@"type" defaultValue:nil operation:NSStringOperationTypeTrim];
            if ([type isEqualToString:@"goodsdetail"]) {
                RCChangePointsExtraItem *item = [RCChangePointsExtraItem parseDataFromJSON:dictItem];
                if (nil == item) {
                    continue;
                }
                [picTempArray addObject:item];
            }else if ([type isEqualToString:@"review"]){
                retObject.reviewItem = [RCChangePointsExtraItem parseDataFromJSON:dictItem];
            }else if ([type isEqualToString:@"changerecord"]){
                retObject.changeRecordItem = [RCChangePointsExtraItem parseDataFromJSON:dictItem];
            }else{
                continue;
            }
        }
        
        retObject.picExtraArray = [NSArray arrayWithArray:picTempArray];
        [picTempArray release];
    }
    
    return [retObject autorelease];
}

@end
